// pages/component/thresItem/thresItem.js
const api = require('../../../apis/request.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    alarm: {
      type: Object,
      value: undefined
    },
    description: {
      type: String,
      value: "0"
    },
    tMax: {
      type: String,
      value: "0"
    },
    tMin: {
      type: String,
      value: "0"
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tempTMax: "",
    tempTMin: "",
    tempTDiff: "",
    old_obj:{}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    saveAlarm: function (event) {
      var _this = this;
      // console.log(JSON.stringify(_this.data.alarm));
      // JSON.stringify(_this.data.alarm)
      _this.data.old_obj = { ..._this.data.alarm }
      var obj_alarm = _this.data.alarm
      obj_alarm.tMax = _this.data.tempTMax || _this.data.old_obj.tMax
      obj_alarm.tMin = _this.data.tempTMin || _this.data.old_obj.tMin

      api.addAlarm(JSON.stringify(obj_alarm) , function (res) {
        console.log(JSON.stringify(res.data));
        var result = res.data;
        if (result.status != 0) {
          wx.showToast({
            title: result.msg
          });

          // if (_this.data.tempTMax != "") {
          //   console.log("tempTMax = " + _this.data.tempTMax);
          //   _this.data.alarm.tMax = _this.data.tempTMax;
          //   _this.data.tempTMax="";
          // }
          // if (_this.data.tempTMin != "") {
          //   console.log("tempTMin = " + _this.data.tempTMin);
          //   _this.data.alarm.tMin = _this.data.tempTMin;
          //   _this.data.tempTMin="";
          // }
          // if (_this.data.tempTDiff != "") {
          //   console.log("tempTDiff = " + _this.data.tempTDiff);
          //   _this.data.alarm.tDiff = _this.data.tempTDiff;
          //   _this.data.tempTDiff="";
          // }
          console.log("alarm = " + JSON.stringify(_this.data.alarm));
          _this.setData({
            alarm: _this.data.alarm
          });

        } else {

          if (_this.data.tempTMax != "") {
            console.log("tempTMax = " + _this.data.tempTMax);
            _this.data.alarm.tMax = _this.data.tempTMax;
            _this.data.tempTMax = "";
          }
          if (_this.data.tempTMin != "") {
            console.log("tempTMin = " + _this.data.tempTMin);
            _this.data.alarm.tMin = _this.data.tempTMin;
            _this.data.tempTMin = "";
          }
          if (_this.data.tempTDiff != "") {
            console.log("tempTDiff = " + _this.data.tempTDiff);
            _this.data.alarm.tDiff = _this.data.tempTDiff;
            _this.data.tempTDiff = "";
          }
          console.log("alarm = " + JSON.stringify(_this.data.alarm));
          _this.setData({
            alarm: _this.data.alarm
          });
        }


      });
    },
    listenTMaxInput: function (e) {
      this.data.tempTMax = e.detail.value;
      // this.data.tempalarm.tMax = e.detail.value;
      // this.setData({
      //   username: e.detail.value
      // })
    },
    listenTMinInput: function (e) {
      this.data.tempTMin = e.detail.value;
      // this.data.tempalarm.tMin = e.detail.value;
      // this.setData({
      //   username: e.detail.value
      // })
    },
    listenTDiffInput: function (e) {
      this.data.tempTDiff = e.detail.value;
      // this.data.tempalarm.tDiff = e.detail.value;
      // this.setData({
      //   username: e.detail.value
      // })
    }
  },
  /**
   * 生命周期
   */
  lifetimes: {
    ready: function () {
      var _this=this;
      var iconPath="";
      switch (_this.data.alarm.groupType) {
        case '43':
          iconPath = "../../../img/elements/icon0_eryanghantan.png";
          break;
        case '48':
          iconPath = "../../../img/elements/icon0_shidu.png";
          break;
        case '49':
          iconPath = "../../../img/elements/icon0_guanzhaoqiangdu.png";
          break;
        case '53':
          iconPath = "../../../img/elements/icon0_shidu_turang.png";
          break;
        case '54':
          iconPath = "../../../img/elements/icon0_wendu.png";
          break;
      }
      _this.setData({ iconPath: iconPath})
    }
  }
})
